// 设计注册用户页面

// 关闭VSCode非必要的警告
// ignore_for_file: must_be_immutable, non_constant_identifier_names, use_build_context_synchronously, camel_case_types

import 'package:flutter/material.dart';
import 'package:project_award_statistics/utils/mobile_material_design/dialogs/register/dialog_register_err_empty.dart';
import 'package:project_award_statistics/utils/mobile_material_design/dialogs/register/dialog_register_err_exists.dart';
import 'package:project_award_statistics/utils/mobile_material_design/dialogs/register/dialog_register_success.dart';
import 'package:project_award_statistics/utils/load_user_info_from_db/load_user_info.dart';
import 'package:project_award_statistics/utils/load_user_info_from_db/user_model.dart';
import 'package:project_award_statistics/utils/mobile_material_design/my_button.dart';

class RegisterPage_Mobile extends StatelessWidget {
  RegisterPage_Mobile({super.key});

  // 先定义两个用户名与密码的文本接口
  TextEditingController controller_new_user=TextEditingController();
  TextEditingController controller_new_password=TextEditingController();

  void do_register (BuildContext context) async   // "注册"按钮触发的进行注册函数
    {
      // 先载入用户信息数据库
      // 先定义UserInfo类对象user_info_list
      List <user_info_model> user_info_list = await DBProvider_UserInfo().get_user_list();
      int i=0;
      // register_user_exists变量用于判断用户注册的用户名是否已经存在
      bool register_user_exists=false; 
      // register_user用于获取用户注册时输入的用户名
      String register_user=controller_new_user.text; 
      // 遍历数据库返回的列表进行查询用户目标注册对象是否存在
      for (i=0;i<user_info_list.length;i++)
        {
          if (user_info_list[i].stu_name==register_user)
            {
              register_user_exists=true;
            }
        }

      if (controller_new_user.text.isEmpty || controller_new_password.text.isEmpty)   // 如果检测到用户未输入注册的姓名/密码,那么进行对应报错处理
        { 
          showDialog(  // 显示报错对话框
            context: context, 
            builder: (context){
              return DialogRegisterErr_Empty();
            }
          );
        }
      else if (register_user_exists)  // 如果用户重复注册
        {
          showDialog(   // 显示用户重复注册报错的对话框
            context: context, 
            builder: (context){
              return DialogRegisterErr_Exists();
            },
          );
        }
      else
        {
          user_info_model register_user_info=user_info_model(
            stu_name: controller_new_user.text, 
            password: controller_new_password.text,
          );
          DBProvider_UserInfo database=DBProvider_UserInfo();
          await database.insert_new_user(register_user_info);

          // 显示注册成功对话框
          showDialog(
            context: context,
            builder: (context){
              return DialogRegisterSuccess(stu_name: controller_new_user.text,);
            },
          );
        }
    }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      resizeToAvoidBottomInset: false,    // 防止安卓设备输入时导致程序显示面积被压缩
      backgroundColor: Theme.of(context).colorScheme.surface, // 设置背景颜色
      appBar:  // 设置页面顶栏
        AppBar(
          backgroundColor: Colors.transparent,
          elevation: 0,
          foregroundColor: Theme.of(context).colorScheme.inversePrimary,
          title: Center(
            child: Text("注册用户页面"), // 设置标题栏文字
          ),
        ),

      body: Padding(       // 通过Padding容器设置与应用边距间隔
        padding: EdgeInsets.only(left: 20.0,right: 20.0,top: 25.0,),
        child: Column(     // 设置行式布局控件
          mainAxisAlignment: MainAxisAlignment.spaceBetween,
          children: [
            Row(      // 先设置列式布局显示西北民大校徽
              mainAxisAlignment: MainAxisAlignment.center,
              children: [
                Image.asset(     //显示校徽
                  "lib/images/northwest_minzu_university_icon.png",
                  width: 100,
                  height: 100,
                ),

                SizedBox(width: 30,),   // 但还是尽量不要用绝对布局,因为不同移动设备的分辨率不一样

                Column(      // 再设置子行式布局上下显示"西北民族大学"与"电子设计工作室"
                  children: [
                    Text(
                      "西北民族大学",
                      style: TextStyle(
                        fontSize: 30,
                        fontWeight: FontWeight.bold,
                      ),
                    ),
                    SizedBox(height: 20,),   //设置上下文字间间隔
                    Text(
                      "电子设计工作室",
                      style: TextStyle(
                        fontSize: 15,
                      ),
                    ),
                  ],
                ),
              ],
            ),

            //显示注册用户界面
            Padding(
              padding: const EdgeInsets.only(left: 12.0,right: 12.0),
              child: Column(     //调用子行列控制而不是全局Column,让输入框布局更加整齐
                children: [   
                  Center(
                    child: TextField(
                      controller: controller_new_user,
                      decoration: InputDecoration(
                        border: OutlineInputBorder(),
                        hintText: "  在这里输入您的姓名 ~",
                      ),
                    ),
                  ),
              
                  SizedBox(height: 40,),
              
                  Center(
                    child: TextField(
                      controller: controller_new_password,
                      obscureText: true, // 设置为true以隐藏输入的文本
                      decoration: InputDecoration(
                        border: OutlineInputBorder(),
                        hintText: "  在这里输入您想使用的密码 ~",
                      ),
                    ),
                  ),
                ],
              ),
            ),
            SizedBox(height: 120,),  //设置控件间距离
            Center(
              child: SizedBox(
                height: 48,
                width: 300,
                child: 
                  MyButton(
                    text: "点击进行注册", 
                    onPressed:()=>do_register(context),
                  ),
              ),
            ),
            SizedBox(height: 0,),  //设置按钮离底部距离
          ],
        ),
      ),  //设置注册页面
    );
  }
}
